package com.ruoyi.edubuy.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.PermissionUtils;
import com.ruoyi.edubuy.domain.ShowShopping;
import com.ruoyi.edubuy.service.IShowShoppingService;
import com.ruoyi.tianliangedu.domain.TianliangCourse;
import com.ruoyi.tianliangedu.service.ITianliangCourseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 购物车XController
 *
 * @author lsj
 * @date 2021-07-13
 */
@Controller
@RequestMapping("/edubuy/shopping")
public class ShowShoppingController extends BaseController {
    private String prefix = "edubuy/shopping";

    @Autowired
    private IShowShoppingService showShoppingService;

    @Autowired
    private ITianliangCourseService tianliangCourseService;

    @RequiresPermissions("edubuy:shopping:view")
    @GetMapping()
    public String shopping() {
        return prefix + "/shopping";
    }

    /**
     * 查询购物车X列表
     */
    @RequiresPermissions("edubuy:shopping:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShowShopping showShopping) {
        startPage();
        List<ShowShopping> list = showShoppingService.selectShowShoppingList(showShopping);
        TianliangCourse tianliangCourse = new TianliangCourse();
        for (int i = 0; i < list.size(); i++) {
            // 获得有名字的商品类
            tianliangCourse.setCourseName(list.get(i).getShoppingShop());
            // 查出名字相同的商品类
            List<TianliangCourse> ls = tianliangCourseService.selectTianliangCourseList(tianliangCourse);
            list.get(i).setCourseId(ls.get(0).getCourseId());
        }
        // 用迭代器删除 不要直接用list
        Iterator<ShowShopping> iterator = list.iterator();
        while (iterator.hasNext()){
            if (!iterator.next().getUserId().equals((String) PermissionUtils.getPrincipalProperty("userName"))) {
                iterator.remove();
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出购物车X列表
     */
    @RequiresPermissions("edubuy:shopping:export")
    @Log(title = "购物车X", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShowShopping showShopping) {
        List<ShowShopping> list = showShoppingService.selectShowShoppingList(showShopping);
        ExcelUtil<ShowShopping> util = new ExcelUtil<ShowShopping>(ShowShopping.class);
        return util.exportExcel(list, "购物车X数据");
    }

    /**
     * 新增购物车X
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存购物车X
     */
    @RequiresPermissions("edubuy:shopping:add")
    @Log(title = "购物车X", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShowShopping showShopping) {
        return toAjax(showShoppingService.insertShowShopping(showShopping));
    }

    /**
     * 修改购物车X
     */
    @GetMapping("/edit/{shoppingId}")
    public String edit(@PathVariable("shoppingId") Long shoppingId, ModelMap mmap) {
        ShowShopping showShopping = showShoppingService.selectShowShoppingById(shoppingId);
        mmap.put("showShopping", showShopping);
        return prefix + "/edit";
    }

    /**
     * 修改保存购物车X
     */
    @RequiresPermissions("edubuy:shopping:edit")
    @Log(title = "购物车X", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShowShopping showShopping) {
        return toAjax(showShoppingService.updateShowShopping(showShopping));
    }

    /**
     * 删除购物车X
     */
    @RequiresPermissions("edubuy:shopping:remove")
    @Log(title = "购物车X", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(showShoppingService.deleteShowShoppingByIds(ids));
    }
}
